The Transmission Control Protocol (“TCP”) provides communication services for transmitting a data stream from a program executing on one device (e.g., server, desktop computer, smartphone) to a program executing on another device. TCP is a transport layer protocol of the Open Systems Interconnection model and typically interfaces with the Internet Protocol (“IP”) of the network layer. The combination of TCP at the transport layer and IP at the network layer is referred to as TCP/IP.
IP provides services for transmitting packets from a device with a source IP address to a device with a destination IP address without establishing a connection and not maintaining state. As a result, IP does not guarantee delivery of the packets. TCP, however, does establish a connection between the source and destination devices and guarantees delivery of the data stream sent as IP packets. TCP uses an acknowledgment protocol to detect when packets are delivered. At the source device, TCP retransmits packets whose delivery is not acknowledged by the destination device. At the destination device, TCP assembles the packets into the data stream and presents the data stream to the destination program identified by a port.
Application programs use higher level protocols, such as the Remote Procedure Call protocol (“RPC”) and the Hyper-Text Transport Protocol (“HTTP”), to send messages using TCP. Since TCP is stream-oriented, it does not understand the semantics of the messages. As a result, application programs need to track message boundaries and compile the data sent as a stream into the messages.
An application program often loops, repeating the process of preparing and sending a message or receiving and processing a message. If an application program is multi-threaded, then multiple threads may need to receive or send messages using the same TCP connection or socket. Because TCP does not understand the semantics of the messages, if the content of messages from different threads is interleaved on the TCP connection, TCP at the destination device will not know which content belongs to which message. As a result, application programs need to ensure that the sending of each message is sent atomically, that is, completes before the next message is sent.
To send messages atomically, the threads of the application program need to coordinate their access to a shared TCP socket. Application programs may coordinate their access using various synchronization techniques such as locking and unlocking of the TCP socket. Such synchronization techniques may place a heavy performance burden on the application programs as waiting threads may need to loop to attempt to lock the TCP socket.